package sunstar.com.xq.presenter

import android.content.Context
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.QueryListListener
import cn.bmob.v3.listener.QueryListener
import cn.bmob.v3.listener.SaveListener
import com.google.gson.Gson
import org.greenrobot.eventbus.EventBus
import sunstar.com.xq.databean.PublicStaticBean
import sunstar.com.xq.databean.UserData
import sunstar.com.xq.util.Preference
import sunstar.com.xq.util.UtilBox
import sunstar.com.xq.view.LoginView

/**
 * Created by Sun.Star on 2017/6/7.
 */
class LoginPresenter(var loginView:LoginView) {
    fun registStart(){
        loginView.registStart()
    }
    fun upDataPasswordStart(){

    }
    fun login(context: Context,userName:String,password:String){
        var user = UserData()
        user.username = userName
        user.setPassword(password)
        user.login(object : SaveListener<UserData>(){
            override fun done(p0: UserData?, p1: BmobException?) {
                if(p1 == null){
                    var string : String by Preference(context,"userData","")
                    string = Gson().toJson(p0)
                    loginView.loginComplete()
                    EventBus.getDefault().post(p0)
                }else{
                    loginView.loginError(p1.message.toString())
                }
            }
        })
    }

    fun check(number:String,passWord:String) : Boolean {
        if(number.equals("")){
            loginView.loginError("请输入手机号")
            return false
        }
        if(passWord.equals("")){
            loginView.loginError("请输入密码")
            return false
        }
        if(!UtilBox.isMobile(number)){
            loginView.loginError("请输入正确手机号")
            return false
        }
        if(passWord.length<6){
            loginView.loginError("请输入大于6位的密码")
            return false
        }
        return true
    }

    fun qqLogin(){

    }
    fun wechatLogin(){

    }
    fun weiboLogin(){

    }
}